package com.yuexunit.finance.support;

import java.sql.Connection;
import java.sql.DriverManager;

public class OracleConnection {

	private String DBDRIVER = "oracle.jdbc.driver.OracleDriver";

	private String DBURL = "jdbc:oracle:thin:@192.168.60.52:1521:atrac2";

	private String DBUSER = "rjkd";

	private String DBPASSWORD = "kingdee";

	private ThreadLocal<Connection> connection = new ThreadLocal<>();

	private static OracleConnection instance;

	public static OracleConnection getInstance() {
		if (instance == null) {
			instance = new OracleConnection();
		}
		return instance;
	}

	/** 获取数据源连接 */
	public Connection getConnection() {
		try {
			Class.forName(DBDRIVER);
			System.out.println("获取数据源驱动成功...");
		} catch (Exception e) {
			System.out.println("获取数据源驱动失败...");
		}
		try {
			Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
			connection.set(conn);
			System.out.println("连接数据源成功...");
		} catch (Exception e) {
			System.out.println("连接数据源失败：" + e.getMessage());
		}
		return connection.get();
	}

	/** 关闭数据源 */
	public void closeConnetion(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
				System.out.println("关闭数据源成功...");
			} catch (Exception e) {
				System.out.println("关闭数据源失败...");
			}
		}
	}
}
